<?php
/**
 * 会员中心
 */

namespace app\home\controller\user;


use app\common\model\UserModel;
use think\Request;
use think\Validate;

class MemeberController extends BaseController
{
    private $userModel;
    private $userInfo;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->userModel=new UserModel();
        $this->userInfo=$this->userModel->getUserInfo($this->getMemberUid());
        $this->assign('user_info',$this->userInfo);
    }

    public function index()
    {
        return $this->fetch();
    }

    public function userInfo()
    {
        return $this->fetch();
    }

    public function safeCenter()
    {
        $level=3;
        if (empty($this->userInfo['password'])){
            $level-=1;
        }
        if (empty($this->userInfo['mobile'])){
            $level-=1;
        }
        if (empty($this->userInfo['email'])){
            $level-=1;
        }
        $this->assign('level',$level);
        return $this->fetch();
    }

    /**
     * 修改密码
     * @return mixed
     */
    public function editPassword()
    {
        if ($this->request->isPost()){
            $data=$this->request->param();
            $validate=new Validate([
                ['old-password','require','请输入旧密码'],
                ['new-password','require','请输入新密码'],
                ['captcha','require|captcha','请输入验证码|验证码输入错误']
            ]);
            if (!$validate->check($data)){
                $this->error($validate->getError());
            }
            if (!$this->userModel->where(['uid'=>$this->getMemberUid(),'password'=>md5($data['old-password'])])->count()){
                $this->error('旧密码输入错误');
            }
            if ($this->userModel->editData(['uid'=>$this->getMemberUid(),'password'=>md5($data['new-password'])])){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        return $this->fetch();
    }

    public function bindMobile()
    {
        if ($this->request->isPost()){
            return true;
        }
        return $this->fetch();
    }

    public function bindEmail()
    {
        if ($this->request->isPost()){
            return true;
        }
        return $this->fetch();
    }
}